var server:WebSocket;
export default({
    connect:(roomId:string, userId:string) => {
        if (server == null) {
            server = new WebSocket(import.meta.env.VITE_BASE_WEB_SOCKET_URL + `/${roomId}` + `/${userId}`);
        }
        return server;
    },
    send:(msg:any) => {
        server.send(JSON.stringify(msg));
    },
    onOpen: (callback:any) => {
        server.addEventListener('open', function (event) {
            callback(event);
        });
    },
    onMessage: (callback:any) => {
        server.addEventListener('message', function (event) {
            callback(event);
        });
    },
    onClose: (callback:any) => {
        server.addEventListener('close', function (event) {
            callback(event);
        });
    },
    onError: (callback:any) => {
        server.addEventListener('error', function (event) {
            callback(event);
        });
    }
})